import { ref, computed, reactive } from 'vue'

// 商品接口定义
export interface FoodItem {
  id: number
  name: string
  description: string
  price: number
  img: string
  quantity: number
}

// 促销活动接口
export interface Promotion {
  type: string
  info: string
}

// 商家信息接口
export interface Business {
  id: number
  name: string
  img: string
  minPrice: number
  deliveryFee: number
  description: string
  rating?: number
  sales?: number
  distance?: number
  deliveryTime?: number
  promotions?: Promotion[]
  quantity?: number
}

// 全局订单状态
const orderState = reactive({
  business: {
    id: 0,
    name: '',
    img: '',
    minPrice: 0,
    deliveryFee: 0,
    description: ''
  } as Business,
  cart: [] as FoodItem[],
  deliveryAddress: {
    address: '云南大学呈贡校区楸苑一栋宿舍楼下',
    contactName: '向日葵女士',
    contactPhone: '18008746522'
  }
})

export function useOrder() {
  // 设置商家信息
  const setBusiness = (business: Business) => {
    orderState.business = { ...business }
  }

  // 设置购物车商品
  const setCartItems = (items: FoodItem[]) => {
    orderState.cart = items.filter(item => item.quantity > 0)
  }

  // 更新商品数量
  const updateItemQuantity = (itemId: number, quantity: number) => {
    const item = orderState.cart.find(item => item.id === itemId)
    if (item) {
      item.quantity = quantity
      if (quantity <= 0) {
        // 如果数量为0，从购物车中移除
        orderState.cart = orderState.cart.filter(item => item.id !== itemId)
      }
    }
  }

  // 添加商品到购物车
  const addToCart = (foodItem: FoodItem) => {
    const existingItem = orderState.cart.find(item => item.id === foodItem.id)
    if (existingItem) {
      existingItem.quantity += 1
    } else {
      orderState.cart.push({ ...foodItem, quantity: 1 })
    }
  }

  // 从购物车移除商品
  const removeFromCart = (itemId: number) => {
    const existingItem = orderState.cart.find(item => item.id === itemId)
    if (existingItem && existingItem.quantity > 0) {
      existingItem.quantity -= 1
      if (existingItem.quantity === 0) {
        orderState.cart = orderState.cart.filter(item => item.id !== itemId)
      }
    }
  }

  // 清空购物车
  const clearCart = () => {
    orderState.cart = []
  }

  // 计算总数量
  const totalQuantity = computed(() => {
    return orderState.cart.reduce((sum, item) => sum + item.quantity, 0)
  })

  // 计算总价格（不含配送费）
  const totalPrice = computed(() => {
    return orderState.cart.reduce((sum, item) => sum + item.price * item.quantity, 0)
  })

  // 计算最终价格（含配送费）
  const finalPrice = computed(() => {
    return totalPrice.value + orderState.business.deliveryFee
  })

  // 获取购物车中的商品列表
  const cartItems = computed(() => orderState.cart)

  // 获取商家信息
  const business = computed(() => orderState.business)

  // 获取配送地址信息
  const deliveryAddress = computed(() => orderState.deliveryAddress)

  return {
    // 状态
    cartItems,
    business,
    deliveryAddress,
    totalQuantity,
    totalPrice,
    finalPrice,
    
    // 方法
    setBusiness,
    setCartItems,
    updateItemQuantity,
    addToCart,
    removeFromCart,
    clearCart
  }
} 